//
//  SSSexPickView.m
//  starspaceVR
//
//  Created by 曹端芹 on 17/10/10.
//  Copyright © 2017年 starspace. All rights reserved.
//

#import "SSSexPickView.h"

@interface SSSexPickView ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic,strong) NSArray *sexArray;
@property (nonatomic,strong) UIPickerView *sexPickView;

@end

@implementation SSSexPickView


- (NSArray *)sexArray {
    if (!_sexArray) {
        _sexArray = @[@"保密",@"男",@"女"];
    }
    return _sexArray;

}
- (instancetype)initWithFrame:(CGRect)frame {
   self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1f];
        [self setup];
    }
    return self;
}

- (void) setup {
    
    
    [self addSubview:self.sexPickView];
    [UIView animateWithDuration:0.25 animations:^{
        self.sexPickView.frame = CGRectMake(0, self.frame.size.height - kRealValue(260), self.frame.size.width, kRealValue(260));
    }];

}

#pragma mark - sexPickerView 代理方法

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 1;
}

- (NSInteger )pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    return self.sexArray.count;
}


-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    
    return 40;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    //设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 1)
        {
            singleLine.backgroundColor = [UIColor redColor];
        }
    }
    
    //设置文字的属性
    UILabel *genderLabel = [UILabel new];
    genderLabel.textAlignment = NSTextAlignmentCenter;
    genderLabel.text = self.sexArray[row];//self.genderArray里边内容为@[@"男",@"女"]
    
    
    return genderLabel;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if ([self.delegate respondsToSelector:@selector(selectedStringInPickView:)]) {
        [self.delegate selectedStringInPickView:self.sexArray[row]];
    }
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismiss];
}

- (void) dismiss {
    
    [UIView animateWithDuration:0.25 animations:^{
        self.sexPickView.frame = CGRectMake(0, self.frame.size.height, self.frame.size.width, kRealValue(260));
    } completion:^(BOOL finished) {
        [self.sexPickView removeFromSuperview];
        [self removeFromSuperview];
    }];

}


- (UIPickerView *)sexPickView {
    if (!_sexPickView) {
        _sexPickView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.frame.size.height, self.frame.size.width, kRealValue(260))];
        _sexPickView.backgroundColor = [UIColor whiteColor];
        _sexPickView.delegate = self;
        _sexPickView.dataSource = self;
    }
    
    return _sexPickView;
}

@end
